# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=db
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=6.0.19
pkgrel=8
pkgdesc="The Berkeley DB embedded database system (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/overview/index.html"
msys2_references=(
  "cpe: cpe:/a:oracle:berkeley_db"
)
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://download.oracle.com/berkeley-db/db-${pkgver}.tar.gz
        0001-db-tls-m4-fix-pthread.patch
        mingw.patch
        clang_cxx_11.patch
        clang_libc++_store.patch
        0002-aarch64-config.patch
        0003-aarch64-mutex-pause.patch)
sha256sums=('2917c28f60903908c2ca4587ded1363b812c4e830a5326aaa77c9879d13ae18e'
            '3b340bfa4817b5cbe1bf295e0cbb87d5a6e16384a16fff485f1e1991e15b1ce0'
            '62981c4064efccbe4219406316151a5266258c270b621f3b3faff39108978d02'
            'd7bc0d56fc20f7e71dca5b4bee21ef296cb6cd4d835ff9789dc61c11f3977325'
            '69be5d13b1377a0bebe767789afc1e9ab34fd6c15c5adcaeec2d5953508ae260'
            '20440d8cdf92bd3de4ecbc16014195ce8c32babede2fbf0dc6f7ed4aea36b452'
            '8d629482bed7af6d9fea371a36a1f3287691db8364aad58b95f30c967aea1bc8')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  _apply_patch_with_msg \
    0001-db-tls-m4-fix-pthread.patch \
    mingw.patch \
    clang_cxx_11.patch \
    clang_libc++_store.patch \
    0002-aarch64-config.patch \
    0003-aarch64-mutex-pause.patch

  pushd dist
  autoreconf -fiv -I aclocal -I aclocal_java
  # they have an extra sed in here that we need
  ./s_config
  popd
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # for ucrt
  export ac_cv_func_time=yes
  export ac_cv_func_localtime=yes

  CFLAGS+=" -DUNICODE -D_UNICODE"
  CXXFLAGS+=" -DUNICODE -D_UNICODE"
  LDFLAGS+=" -lpthread"
  ../${_realname}-${pkgver}/dist/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-compat185 \
    --enable-mingw \
    --enable-shared \
    --enable-static \
    --disable-rpath \
    --enable-cxx \
    --enable-sql \
    --enable-sql-codegen \
    --enable-stl \
    --enable-dbm \
    --disable-tcl \
    --disable-replication \
    --docdir='${prefix}/share/doc/db'

  make LIBSO_LIBS=-lpthread
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make cutest
  if [ -x ./cutest.exe ]; then
    ./cutest.exe || true
  fi
}

package_db() {
  make DESTDIR="${pkgdir}" -C "${srcdir}/build-${MSYSTEM}" install
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_db-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
